package com.ruike.alisurface.ui.setting;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;

import com.ruike.alisurface.Constant;
import com.ruike.alisurface.R;
import com.ruike.alisurface.Serials.MsgEventBus;
import com.ruike.alisurface.base.BaseAty;
import com.ruike.alisurface.utils.DeviceUtils;
import com.ruike.alisurface.utils.ShareKey;
import com.voodoo.lib_utils.L;
import com.voodoo.lib_utils.ShareUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: voodoo
 * CreateDate: 2020-04-16 016 下午 04:41
 * Description: 基础设置
 */
public class BasicSettingActivity extends BaseAty {

    @BindView(R.id.baseSetting_buyQuantity_et)
    EditText buyQuantityEt;
    @BindView(R.id.baseSetting_weichat_swch)
    Switch weichatSwch;
    @BindView(R.id.baseSetting_Alface_swch)
    Switch alchatSwch;
    @BindView(R.id.baseSetting_showAdv_swch)
    Switch showAdvSwch;
    @BindView(R.id.baseSetting_stopSell_swch)
    Switch stopSellSwch;
    @BindView(R.id.baseSetting_showGoodsInfo_swch)
    Switch showGoodsInfoSwch;
    @BindView(R.id.baseSetting_offlineRestart_swch)
    Switch offlineRestartSwch;
    @BindView(R.id.baseSetting_showNavigationBar_swch)
    Switch showNavigationBarSwch;
    @BindView(R.id.baseSetting_shop_type)
    Switch baseSettingShopType;

    @BindView(R.id.baseSetting_isAlface_swch)
    Switch alFaceSwch;

    @BindView(R.id.baseSetting_offlineRestart_et)
    EditText Restart_et;
    @BindView(R.id.baseSetting_offlineRestart_btn)
    Button Restart_btn;
    @BindView(R.id.baseSetting_showGoods_direction_swch)
    Switch baseSettingShowGoodsDirectionSwch;
    @BindView(R.id.baseSetting_showGoods_number_swch)
    Switch baseSettingShowGoodsNumberSwch;

    int purchaseCount; // 设置的购买数量

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_setting_base;
    }

    @Override
    protected void initViews() {
        initTitleBar(false);
    }

    int IS_SHOP_TYPE;

    @Override
    protected void initData() {
        // 购买数量
        purchaseCount = ShareUtils.getInstance().getInt(ShareKey.PURCHASE_COUNT, Constant.DefaultData.PURCHASE_COUNT);
        buyQuantityEt.setText(String.valueOf(purchaseCount));
        int isShowAdv = ShareUtils.getInstance().getInt(ShareKey.ADV_TYPE); // 展示界面广告
        showAdvSwch.setChecked(isShowAdv != Constant.ADV_TYPE_NONE); // 无广告为2，showAdvSwch状态为关

        boolean isStopSelling = ShareUtils.getInstance().getBoolean(ShareKey.IS_STOP_SELLING); // 停止售卖
        stopSellSwch.setChecked(isStopSelling);
        boolean isOfflineRestartApp = ShareUtils.getInstance().getBoolean(ShareKey.IS_OFFLINE_RESTART_APP); // 断网重启app
        offlineRestartSwch.setChecked(isOfflineRestartApp);
        boolean isShowGoodsInfo = ShareUtils.getInstance().getBoolean(ShareKey.IS_SHOW_GOODS_INFO); // 可展示商品详情
        showGoodsInfoSwch.setChecked(isShowGoodsInfo);
        IS_SHOP_TYPE = ShareUtils.getInstance().getInt(ShareKey.IS_SHOP_TYPE, 1); // 可商品类型展示

        baseSettingShopType.setChecked(IS_SHOP_TYPE != 1);

        int restCount = ShareUtils.getInstance().getInt(ShareKey.IS_OFFLINE_RESTART_APPCOUNT, 10);
        Restart_et.setText(String.valueOf(restCount));

        weichatSwch.setChecked(ShareUtils.getInstance().getBoolean(ShareKey.IS_OPEN_WEALIPAY_PAY, true)); // 启用  扫码 支付

        alchatSwch.setChecked(ShareUtils.getInstance().getBoolean(ShareKey.IS_OPEN_ALIPAY_PAY, true));// 启用 支付宝刷脸支付
        showNavigationBarSwch.setChecked(ShareUtils.getInstance().getBoolean(ShareKey.IS_SHOW_NAVIGATION_BAR, false)); // 设置是否显示底部导航栏
        isFace = ShareUtils.getInstance().getBoolean(ShareKey.IS_FACE, false);
        setSwUI(isFace);
        boolean gooods_direction = ShareUtils.getInstance().getBoolean(ShareKey.IS_GOODS_DIRECTION, false);
        baseSettingShowGoodsDirectionSwch.setChecked(gooods_direction);
        boolean gooods_number = ShareUtils.getInstance().getBoolean(ShareKey.IS_GOODS_NUMBER, false);
        baseSettingShowGoodsNumberSwch.setChecked(gooods_number);


    }

    @OnClick({R.id.baseSetting_goback_imgv, R.id.baseSetting_buyQuantitySave_btn, R.id.baseSetting_isAlface_swch,
            R.id.baseSetting_weichat_swch, R.id.baseSetting_Alface_swch, R.id.baseSetting_stopSell_swch,
            R.id.baseSetting_showGoodsInfo_swch, R.id.baseSetting_offlineRestart_swch, R.id.baseSetting_showNavigationBar_swch,
            R.id.baseSetting_shop_type, R.id.baseSetting_showAdv_swch, R.id.baseSetting_offlineRestart_btn,
            R.id.baseSetting_showGoods_direction_swch, R.id.baseSetting_showGoods_number_swch,
    })
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.baseSetting_goback_imgv:
                if (stopSellSwch.isChecked()) {
                    startActivity(StopSellingActivity.class, null);
                } else {
                    finish();
                }
                break;
            case R.id.baseSetting_buyQuantitySave_btn:
                String buyQuantityStr = buyQuantityEt.getText().toString().trim();
                if (!buyQuantityStr.isEmpty()) {
                    try {
                        purchaseCount = Integer.parseInt(buyQuantityStr);
                    } catch (Exception e) {
                        showErrorTip(getResources().getString(R.string.onlyInputIntNumStr));
                        return;
                    }
                    ShareUtils.getInstance().putInt(ShareKey.PURCHASE_COUNT, purchaseCount); // 购买数量
                    showRightTip(getResources().getString(R.string.setSuccessStr));
                } else {
                    showErrorTip(getResources().getString(R.string.pleaseInputBuyNumStr));
                }
                break;
            case R.id.baseSetting_offlineRestart_btn:
                String Restart_etyStr = Restart_et.getText().toString().trim();
                if (!Restart_etyStr.isEmpty()) {
                    try {
                        int buyQuantity = Integer.parseInt(Restart_etyStr);
                        if (buyQuantity >= 0 && buyQuantity <= 100) {
                            ShareUtils.getInstance().putInt(ShareKey.IS_OFFLINE_RESTART_APPCOUNT, buyQuantity);
                            showRightTip(getResources().getString(R.string.setSuccessStr));
                        } else { // 小于0 或者大于100
                            showErrorTip("设置的重启次数过大，最多100次");
                            Restart_et.setText(buyQuantity <= 0 ? String.valueOf(1) : String.valueOf(10));
                        }
                    } catch (Exception e) {
                        showErrorTip("");
                    }
                } else {
                    showErrorTip("");
                }
                break;

            case R.id.baseSetting_showAdv_swch:
                ShareUtils.getInstance().putInt(ShareKey.ADV_TYPE, showAdvSwch.isChecked() ? Constant.ADV_TYPE_IMAGE : Constant.ADV_TYPE_NONE); // 选中的话为轮播图广告，关闭的话状态为2
                showRightTip(getResources().getString(R.string.setSuccessStr));
                break;
            case R.id.baseSetting_weichat_swch:
                ShareUtils.getInstance().putBoolean(ShareKey.IS_OPEN_WEALIPAY_PAY, weichatSwch.isChecked()); // 启用微信支付宝支付
                showRightTip(getResources().getString(R.string.setSuccessStr));
                break;
            case R.id.baseSetting_Alface_swch:
                ShareUtils.getInstance().putBoolean(ShareKey.IS_OPEN_ALIPAY_PAY, alchatSwch.isChecked()); // 启用 支付宝刷脸支付
                showRightTip(getResources().getString(R.string.setSuccessStr));
                break;
            case R.id.baseSetting_stopSell_swch:
                ShareUtils.getInstance().putBoolean(ShareKey.IS_STOP_SELLING, stopSellSwch.isChecked()); // 停止售卖
                showRightTip(getResources().getString(R.string.setSuccessStr));
                break;
            case R.id.baseSetting_showGoodsInfo_swch:
                ShareUtils.getInstance().putBoolean(ShareKey.IS_SHOW_GOODS_INFO, showGoodsInfoSwch.isChecked()); // 可展示商品详情
                showRightTip(getResources().getString(R.string.setSuccessStr));
                break;
            case R.id.baseSetting_offlineRestart_swch:
                ShareUtils.getInstance().putBoolean(ShareKey.IS_OFFLINE_RESTART_APP, offlineRestartSwch.isChecked()); // 断网重启app
                showRightTip(getResources().getString(R.string.setSuccessStr));
                break;
            case R.id.baseSetting_shop_type:
                ShareUtils.getInstance().putInt(ShareKey.IS_SHOP_TYPE, IS_SHOP_TYPE == 1 ? 2 : 1); //  按货道展示：按商品类型展示
                showRightTip(getResources().getString(R.string.setSuccessStr));
                break;
            case R.id.baseSetting_showNavigationBar_swch:
                DeviceUtils.showNavigationBar(showNavigationBarSwch.isChecked());
                ShareUtils.getInstance().putBoolean(ShareKey.IS_SHOW_NAVIGATION_BAR, showNavigationBarSwch.isChecked()); // 显示界面底部导航栏
                showRightTip(getResources().getString(R.string.setSuccessStr));
                break;

            case R.id.baseSetting_isAlface_swch:
                L.i(" alFaceSwch.isChecked()==", alFaceSwch.isChecked());
                ShareUtils.getInstance().putBoolean(ShareKey.IS_FACE, alFaceSwch.isChecked()); // 设置是否以刷脸为主
                showRightTip(getResources().getString(R.string.setSuccessStr));
                setSwUI(alFaceSwch.isChecked());
                break;
            case R.id.baseSetting_showGoods_direction_swch:
                L.i(" baseSettingShowGoodsDirectionSwch.isChecked()==", baseSettingShowGoodsDirectionSwch.isChecked());
                ShareUtils.getInstance().putBoolean(ShareKey.IS_GOODS_DIRECTION, baseSettingShowGoodsDirectionSwch.isChecked()); // 设置是否以刷脸为主
                showRightTip(getResources().getString(R.string.setSuccessStr));

                break;
            case R.id.baseSetting_showGoods_number_swch:
                L.i(" baseSettingShowGoodsNumberSwch.isChecked()==", baseSettingShowGoodsNumberSwch.isChecked());
                ShareUtils.getInstance().putBoolean(ShareKey.IS_GOODS_NUMBER, baseSettingShowGoodsNumberSwch.isChecked()); // 设置是否以刷脸为主
                showRightTip(getResources().getString(R.string.setSuccessStr));

                break;

            default:
                break;
        }
    }

    @Override
    public void doEventBusResult(MsgEventBus event) {


    }

    public void setSwUI(boolean isFace) {
        if (isFace) {
            alFaceSwch.setChecked(true);
            weichatSwch.setVisibility(View.GONE);
            alchatSwch.setVisibility(View.GONE);
        } else {
            alFaceSwch.setChecked(false);
            weichatSwch.setVisibility(View.VISIBLE);
            alchatSwch.setVisibility(View.VISIBLE);
        }
    }


}